package ai.people.platform.system.controller;

import ai.people.netmon.framework.domain.system.entity.NmSystemUser;
import ai.people.netmon.framework.domain.system.entity.NmSystemUserInfo;
import ai.people.netmon.framework.domain.system.request.NmSystemAddUserParam;
import ai.people.netmon.framework.domain.system.request.NmSystemUpdateUserParam;
import ai.people.netmon.framework.domain.system.request.SystemUserRequest;
import ai.people.netmon.framework.domain.system.request.UpdatePasswordParam;
import ai.people.netmon.framework.domain.system.vo.NmSystemLoginUserVO;
import ai.people.netmon.framework.domain.system.vo.NmSystemUserSimpleVO;
import ai.people.netmon.framework.domain.system.vo.NmSystemUserVO;
import ai.people.netmon.framework.domain.ucenter.entity.NmSysUser;
import ai.people.netmon.framework.model.response.ListResponse;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.platform.system.service.SystemUserService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 系统用户接口
 *
 * @author yuanqinglong
 * @date 2022/3/4 15:47
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class SystemUserController {


    private final SystemUserService systemUserServiceImpl;


    /**
     * 通过账号查询用户
     *
     * @param account 账号
     * @return {@link Result}<{@link NmSysUser}>
     */
    @GetMapping("/{account}")
    public Result<NmSystemUserVO> getUserByAccount(@PathVariable("account") String account) {
        NmSystemUserVO userByAccount = systemUserServiceImpl.getUserByAccount(account);
        return ResultUtil.success(userByAccount);
    }


    /**
     * 通过账号更新密码
     * @ignore
     * @param updatePasswordParam 更新密码参数
     * @return {@link Result}<{@link NmSystemUserVO}>
     */
    @PostMapping("/updatePassword")
    public Result<NmSystemUserVO> updatePasswordByAccount(@Validated @RequestBody UpdatePasswordParam updatePasswordParam) {
        NmSystemUserVO systemUserVO = systemUserServiceImpl.updatePasswordByAccount(updatePasswordParam);
        return ResultUtil.success(systemUserVO);
    }


    /**
     * 通过账号查询用户、角色、菜单
     * @ignore
     * @param account 账号
     * @return {@link Result}<{@link NmSysUser}>
     */
    @GetMapping("/login/account")
    public Result<NmSystemLoginUserVO> getLonginUserByAccount(@RequestParam("account") String account) {
        NmSystemLoginUserVO userByAccount = systemUserServiceImpl.getLonginUserByAccount(account);
        return ResultUtil.success(userByAccount);
    }


    /**
     * 非管理员用户列表
     *
     * @return {@link Result}<{@link ListResponse}<{@link NmSystemUserSimpleVO}>>
     */
    @GetMapping("/nonAdminUsers")
    public Result<ListResponse<NmSystemUserSimpleVO>> listNonAdminUsers() {
        List<NmSystemUserSimpleVO> nonAdminUsers = systemUserServiceImpl.listNonAdminUsers();
        return ResultUtil.success(ListResponse.builder(nonAdminUsers));
    }


    /**
     * 分页用户列表
     *
     * @param systemUserRequest 系统用户请求
     * @return {@link Result}<{@link PageResponse}<{@link NmSystemUserVO}>>
     */
    @PostMapping("/listUsersPage")
    public Result<PageResponse<NmSystemUserVO>> listUsersPage(@RequestBody SystemUserRequest systemUserRequest) {
        PageResponse<NmSystemUserVO> listUsers = systemUserServiceImpl.listUsers(systemUserRequest);
        return ResultUtil.success(listUsers);
    }


    /**
     * 添加系统用户
     *
     * @param nmSystemAddUserParam 添加系统用户参数
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping
    public Result<?> addSystemUser(@Validated @RequestBody NmSystemAddUserParam nmSystemAddUserParam) {
        NmSystemUser nmSystemUser = JSON.parseObject(JSON.toJSONString(nmSystemAddUserParam), NmSystemUser.class);
        NmSystemUserInfo nmSystemUserInfo = JSON.parseObject(JSON.toJSONString(nmSystemAddUserParam), NmSystemUserInfo.class);
        systemUserServiceImpl.addSystemUser(nmSystemUser, nmSystemUserInfo);
        return ResultUtil.success();
    }


    /**
     * 修改系统用户
     *
     * @param nmSystemUpdateUserParam 修改用户参数
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping
    public Result<?> updateSystemUser(@Validated @RequestBody NmSystemUpdateUserParam nmSystemUpdateUserParam) {
        NmSystemUser nmSystemUser = JSON.parseObject(JSON.toJSONString(nmSystemUpdateUserParam), NmSystemUser.class);
        NmSystemUserInfo nmSystemUserInfo = JSON.parseObject(JSON.toJSONString(nmSystemUpdateUserParam), NmSystemUserInfo.class);
        systemUserServiceImpl.updateUser(nmSystemUser, nmSystemUserInfo);
        return ResultUtil.success();
    }


    /**
     * 通过id查询用户
     *
     * @param id id
     * @return {@link Result}<{@link NmSystemUserVO}>
     */
    @GetMapping("/getUser")
    public Result<NmSystemUserVO> getUserById(@NotNull(message = "用户id不可为空") Long id) {
        return ResultUtil.success(systemUserServiceImpl.getUserById(id));
    }


    /**
     * 通过id删除用户
     *
     * @param id id
     * @return {@link Result}<{@link ?}>
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteUser(@PathVariable("id") Long id) {
        systemUserServiceImpl.deleteUser(id);
        return ResultUtil.success();
    }


    /**
     * 更新最后登录时间
     *
     * @ignore
     * @param userId 用户id
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/updateLoginTime")
    public Result<?> updateLastLoginTime(@RequestParam("userId") Long userId) {
        systemUserServiceImpl.updateLastLoginTime(userId);
        return ResultUtil.success();
    }






}
